<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2025 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\model\UserDao;
use app\service\OfficeAccount;
use app\service\WeChat;
use app\service\Weixin;
use think\admin\Controller;
use think\admin\extend\JwtExtend;

class User extends Controller
{
    public function authorize()
    {
        $code = $this->request->param('code');
        if (empty($code)) {
            $url = url('user/authorize', [], false, config('wechat.notify_host'));
            $service = new OfficeAccount();
            $jumpUrl = $service->getAuthCode($url);
            $this->redirect($jumpUrl);
        }
        $this->success('ok', ['code' => $code]);
    }

    public function login()
    {
        $data = $this->_vali([
            'code.require' => 'code不能为空',
        ]);
        $code = $data['code'];
        $service = new Weixin();
        $ret = $service->getOpenid($code);
        //p($ret);
        if (empty($ret['openid'])) {
            $this->error($ret['errmsg'] ?? '获取微信信息失败！');
        }
        $data['openid'] = $ret['openid'];
        $session_key = $ret['session_key'];
        $info = UserDao::where('openid', $data['openid'])->find();
        if (empty($info)) {
            $info = new UserDao([
                'openid' => $data['openid'],
            ]);
            $info->save();
        }
        $uid = $info->id;
        //$token=JwtExtend::token(['uid'=>$uid]);
        $token = createToken($uid);
        $info->token = $token;
        $info->last_login = date('Y-m-d H:i:s');
        cache('login:' . $uid, $token, 86400);
        $info->save();
        $data = [
            'uid' => $uid,
            'session_key' => $session_key,
            'token' => $token,
        ];
        $this->success('登录成功！', $data);
    }

    public function mobile()
    {
        $data = $this->_vali([
            'code.require' => '获取手机号的授权码不能为空',
        ]);
        $wechat = new  Weixin();
        $mobile = $wechat->getMobile($data['code']);
        $uid = $this->request->uid;
        $ret = compact('mobile');
        UserDao::where('id', $uid)->update($ret);
        $this->success('ok！', $ret);
    }

    public function save()
    {
        $data = $this->_vali([
            'name.require' => '姓名不能为空',
            'name.max:100' => '姓名不能超过100个字',
            'nickname.require' => '昵称不能为空',
            'mobile.require' => '手机号不能为空',
            'nickname.max:200' => '昵称不能超过200个字',
            'avatar.require' => '头像不能为空',
            'avatar.url' => '头像格式无效',
        ]);
        $user = $this->request->user;
        if ($user->save($data)) {
            $this->success('ok');
        }
        $this->error('保存失败！');
    }

    public function info()
    {
        $user = $this->request->user;
        $this->success('ok', [
            'name' => $user->name,
            'nickname' => $user->nickname,
            'avatar' => $user->avatar,
            'mobile' => $user->mobile,
        ]);
    }

}
